export type DelayManager = Promise<void> & {
  stop: (ok?: boolean) => void
}

/**
 *
 * @param timeout 需要推迟的时间
 * @returns
 */
export function delay(timeout: number): DelayManager {
  let t = 0;
  let reject: () => void;
  let resolve: () => void;
  let p: Promise<void> = new Promise((_resolve, _reject) => {
    resolve = _resolve;
    reject = _reject;
    t = setTimeout(() => resolve(), timeout);
  });
  (p as DelayManager).stop = (ok: boolean = false) => {
    clearTimeout(t);
    if (ok) {
      resolve();
    } else {
      reject();
    }
  }
  return (p as DelayManager);
}

export function assert(
  condition: boolean,
  message: string = "assert failed!"
): asserts condition {
  if (!condition) {
    // throw new CMError(message, ErrorCode.Error);
  }
}
